//
//  CustomCollectionViewCell.m
//  XMJJ_zml
//
//  Created by 杨何华 on 2025/5/8.
//

#import "CustomCollectionViewCell.h"
#import "SDWebImageManager.h"
@class ZMYCommend;
@implementation CustomCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化图片视图
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.bgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:self.bgView];
        
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 20;
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-100)];
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.imageView.layer.masksToBounds = YES;
        self.imageView.layer.cornerRadius = 20;
//        UIViewContentModeScaleToFill,
//        UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
//        UIViewContentModeScaleAspectFill,
        [self.bgView addSubview:self.imageView];
        // 初始化文本标签
        self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.imageView.frame)+10, frame.size.width-20, 40)];
        self.textLabel.numberOfLines = 2;
        self.textLabel.textColor  = kColorHEX(0x333333);
        self.textLabel.textAlignment = NSTextAlignmentLeft;
        self.textLabel.font = [UIFont systemFontOfSize:16];
        [self.bgView addSubview:self.textLabel];
        self.textLabel.text = @"123";
        
        
        self.userImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.textLabel.frame)+10, 30, 30)];
        self.userImage.contentMode = UIViewContentModeScaleAspectFill;
        self.userImage.layer.masksToBounds = YES;
        self.userImage.layer.cornerRadius = 15;
        [self.bgView addSubview:self.userImage];
 
        self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.userImage.frame)+5, self.userImage.y, frame.size.width-20, 30)];
        self.userLabel.textColor  = [UIColor blackColor];
        self.userLabel.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.userLabel];
        self.userLabel.text = @"123";
        
        self.commend = [UIButton buttonWithType:UIButtonTypeCustom];
        self.commend.frame = CGRectMake(frame.size.width-50,self.userLabel.y, 50, 30);
        [self.commend setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.bgView addSubview:self.commend];
        self.commend.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.commend setTitle:@"110" forState:UIControlStateNormal];
        
     }
    return self;
}
-(void)setData:(ZMYCommend *)data{
    _data = data;
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    self.textLabel.text = data.title;
    self.userLabel.text = data.nickname;
    NSString *text = data.title;
    UIFont *font = [UIFont systemFontOfSize:15.0];
     // 计算多行文本高度
    CGSize maxSize = CGSizeMake(300, CGFLOAT_MAX);
    CGRect rect = [text boundingRectWithSize:maxSize
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: font}
                                     context:nil];
    CGFloat height = rect.size.height;
    self.textLabel.height = height + 5;
    [self setNeedsLayout];
    [manager loadImageWithURL:[NSURL URLWithString:data.coverImageUrl] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        self.imageView.image = image;
//        self.imageView.height = image.size.height/2;
        
    }];
    
    [manager loadImageWithURL:[NSURL URLWithString:data.coverImageUrl] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        self.userImage.image = image;
        
    }];
    
   
}
@end
